from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core import validators
from formula.public_model.universal import sonw17


# Create your models here.

class UserProfile(AbstractUser):
    """
    用户信息
    """
    gender_tuple = (("male", "男"), ("female", "女"))
    id = models.AutoField(verbose_name="自增id", help_text="自增id", primary_key=True, unique=True, db_column="id",
                          db_index=True)
    user_code = models.CharField(verbose_name="用户id", help_text="用户id", max_length=255, unique=True,
                                 db_index=True, db_column="user_code", default=sonw17)
    username = models.CharField(verbose_name="用户名", help_text="用户名", max_length=20, null=True, blank=True, unique=True)
    mobile = models.CharField(verbose_name="手机号码", help_text="手机号码", max_length=11, unique=True, db_index=True,
                              validators=[validators.RegexValidator(r"^1[3-9]\d{9}$", message='请输入正确手机号码')]
                              )
    birthday = models.DateField(verbose_name="生日", help_text="生日", null=True, blank=True)
    gender = models.CharField(verbose_name="性别", help_text="性别", max_length=6, choices=gender_tuple, default="")
    email = models.EmailField(verbose_name="邮箱", help_text="邮箱", max_length=100, null=True, blank=True,
                              db_index=True, default="")
    tag = models.CharField(verbose_name="所属行业", help_text="所属行业", max_length=10, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name="数据创建时间", help_text="数据创建时间", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="数据更新时间", help_text="数据更新时间", auto_now=True)
    # USERNAME_FIELDS = 'username' # 用户名字段
    REQUIRED_FIELDS = ["email", "mobile"]  # 必填字段

    class Meta:
        verbose_name = "用户信息表"
        verbose_name_plural = verbose_name
        db_table = "t_user_info"
        ordering = ['-create_time']

    def __str__(self):
        return self.username
